<template>
	<view class="mainpadding beijingse">
		<!-- 展示地址 -->
		<view class="ffffff mainpadding radius mainpadding" v-if="dataAll.addressData!=null" @click="changeaddress">
			<view class="flexbetween">
				<view class="">
					<view class="flexleft">
						<view class="titletext margin_right3">{{dataAll.addressData.name}}</view>
						<view class="titletext">{{dataAll.addressData.mobile}}</view>
					</view>
					<view class="margin_top2 huisetext">
						{{dataAll.addressData.cityinfo+dataAll.addressData.address_detail}}</view>
				</view>
				<view class="righticon"></view>
			</view>
		</view>
		<!-- 添加地址 -->
		<view class="biabox flexcenter ffffff radius" v-if="dataAll.addressData==null" @click="changeaddress">
			<view class="textcenter dhtext">点击添加收货地址</view>
		</view>
		<view v-for="item in dataAll.orderData.lists" :key="item.shop_id">
			<view class="ffffff mainpadding radius margin_top">
				<view class="flexleft">
					<view class="haungfz margin_right1"></view>
					<view class="titletext margin_right2">{{item.shop_name}}</view>
					<view class="righticon"></view>
				</view>
				<view class="flexbetween margin_top" v-for="ite in item.products" :key="ite.id">
					<view class="shilitu">
						<image :src="ite.image_text" mode=""></image>
					</view>
					<view class="" style="width: 74%;">
						<view class="ershib yhxiank">{{ite.title}}</view>
						<view class="huisetext yhxiank">{{ite.sku.difference.join(",")}}</view>
						<view class="flexbetween margin_top1">
							<!-- 普通购买价格 -->
							<view class="qianshu" v-if="lastmsg.buy_type==1 && lastmsg.goods_type==1">¥{{ite.sku.price}}
							</view>
							<!-- 返利购买价格 -->
							<view class="qianshu" v-if="lastmsg.buy_type==2 && lastmsg.goods_type==1">
								¥{{ite.sku.fl_price}}</view>
							<!-- 福利券商品 -->
							<view class="qianshu" v-if="lastmsg.goods_type==2">
								{{ite.sku.fl_price}}福利券+{{ite.sku.price}}现金</view>
							<view class="shuliang">*{{ite.number}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="ffffff mainpadding radius margin_top">
				<view class="flexbetween">
					<view class="strongtext">商品金额</view>
					<view class="qianshu" v-if="lastmsg.goods_type==1">¥{{item.order_price}}</view>
					<view class="qianshu" v-if="lastmsg.goods_type==2">{{item.sub_fl_price}}福利券+{{item.order_price}}现金
					</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="strongtext">运费</view>
					<view class="huisetext">¥{{item.freight_price_bak}}</view>
				</view>
			</view>
			<view class="margin_top mainpadding ffffff radius">
				<input type="text" placeholder="请输入您的留言内容" v-model="item.remark">
			</view>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb ffffff mainpadding">
			<view class="flexbetween">
				<view class="flexleft" v-if="lastmsg.goods_type==1">
					<view class="titletext">合计：</view>
					<view class="daqiantext">¥{{dataAll.orderData.statis.allsub}}</view>
				</view>
				<view class="flexleft" v-if="lastmsg.goods_type==2">
					<view class="strongtext">合计：</view>
					<view class="qianshu">
						¥{{dataAll.orderData.statis.allflsub}}福利券+{{dataAll.orderData.statis.allsub}}现金</view>
				</view>
				<view class="xiaohuangbtn" @click="tzzftm" v-if="lastmsg.goods_type==1">立即支付</view>
				<view class="xiaohuangbtn" @click="tzzftm" v-if="lastmsg.goods_type==2">立即兑换</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				lastmsg: {
					buy_type: "", // 1普通商品,2返利商品
					type: '',
					data: [],
					goods_type: 1 //1普通商品，2福利券商品
				},
				addressid: "",
				dataAll: {
					"addressData": {
						"address_name": null,
						"address_detail": "",
						"name": "",
						"mobile": "",
						"cityinfo": ""
					},
					"orderData": {
						"lists": [{
							"shop_id": 1,
							"shop_name": "",
							"products": [{
								"id": 3,
								"shop_id": 1,
								"shop_category_id": null,
								"activity_type": "",
								"title": "",
								"image": "",
								"stock": "",
								"freight_id": 3,
								"sales": 0,
								"specs": "multi",
								"freight": {
									"id": 3,
									"delivery": "",
									"isdelivery": "",
									"name": "",
									"valuation": "",
									"price": ""
								},
								"sku": {
									"id": 85,
									"goods_id": 3,
									"difference": [],
									"price": "",
									"market_price": "",
									"fl_price": "",
									"stock": 0,
									"weigh": ""
								},
								"number": "",
								"shop": {
									"name": "",
									"status_text": "",
									"create_time_text": "",
									"update_time_text": "",
									"logo_text": "",
									"like": 1,
									"service_data": [{
										"name": "",
										"description": "",
										"status_text": ""
									}]
								},
								"image_text": ""
							}],
							"coupon": [],
							"freight": {
								"id": 3,
								"name": "",
								"price": ""
							},
							"number": "",
							"sub_price": "",
							"order_price": "",
							"freight_price_bak": ""
						}],
						"statis": {
							"allnum": 0,
							"allsub": "",
							allflsub: ""
						}
					}
				},

			}
		},
		onLoad(options) {
			this.lastmsg = JSON.parse(decodeURIComponent(options.msg));
			console.log(this.lastmsg, "界面接收参数")
		},
		onShow() {
			uni.$off('addressId')
			uni.$on('addressId', data => {
				this.addressid = data
			})
			this.getdingdan()
		},
		methods: {
			getdingdan() {
				let url = ""
				let data = {
					buy_type: this.lastmsg.buy_type, //购买类型1普通购买，2返利购买
					address_id: this.addressid, //地址ID
					type: this.lastmsg.type, //来源类型detail详情，car购物车
					data: this.lastmsg.data //数据列表
				}
				if (this.lastmsg.goods_type == 1) {
					url = "/api/order/getOrderGoodsList"
				} else {
					url = "/api/order/getOrderGoodsList1"
					delete data.buy_type
				}
				httpRequest.request(url, 'POST', data, false, false, true).then(res => {
					if(res.code==1){
						res.data.orderData.lists.forEach(item => {
							this.$set(item, "remark", '')
						})
						this.dataAll = res.data
						if(res.data.addressData){
							this.addressid = res.data.addressData.id
						}
					}else{
						httpRequest.toast(res.msg);
					}
					
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},

			// 跳转支付页面
			tzzftm() {
				if (this.addressid == "") {
					httpRequest.toast("请选择地址")
					return
				}
				uni.showLoading({
					title: '请稍候...'
				})
				if (this.lastmsg.type == "cart") {
					this.delcar()
				}
				let lists = []
				this.dataAll.orderData.lists.forEach(item => {
					let arr = []
					item.products.forEach(ite => {
						arr.push({
							goods_id: ite.id,
							number: ite.number,
							sku_id: ite.sku.id
						})
					})
					lists.push({
						shop_id: item.shop_id,
						remarks: item.remark,
						products: arr
					})
				})
				let url = ""
				let data = {
					address_id: this.addressid, //地址ID
					buy_type: this.lastmsg.buy_type, //购买类型1普通购买，2返利购买
					lists: lists
				}
				if (this.lastmsg.goods_type == 1) {
					url = "/api/order/addOrder"
				} else {
					url = "/api/order/addOrder1"
					delete data.buy_type
				}
				httpRequest.request(url, 'POST', data, false, false, true).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						if (this.lastmsg.goods_type == 1) {
							let obj = {
								buy_type: this.lastmsg.buy_type,
								order_pay_no: res.data.order_pay_no,
								pay_price: res.data.pay_price,
							}
							uni.navigateTo({
								url: '/pages/homepage/querenddzhzf?msg=' + encodeURIComponent(JSON
									.stringify(obj))
							})
						} else {
							let obj = {
								fl_price: res.data.fl_price,
								order_pay_no: res.data.order_pay_no,
								pay_price: res.data.pay_price,
							}
							uni.navigateTo({
								url: '/pages/homepage/querenddzhzfflq?msg=' + encodeURIComponent(JSON
									.stringify(obj))
							})
						}

					} else {
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					uni.hideLoading()
					httpRequest.toast(err.msg);
				})
			},
			delcar() {
				let arr = this.lastmsg.data
				arr.forEach(item => {
					delete item.number
				})
				httpRequest.request('/api/cart/delCart', 'POST', {
					data: arr,
				}).then(res => {
				})
			},
			// 跳转收货地址
			changeaddress() {
				uni.navigateTo({
					url: '/pages/mypage/shouhuodiz'
				})
			},
			// 切换地址
			changeaddress() {
				uni.navigateTo({
					url: "/pages/mypage/shouhuodiz?type=1"
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.daqiantext {
		font-size: 36rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FAB536;
	}

	.ershib {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.qianshu {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FAB536;
	}

	.shuliang {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #999999;
	}

	.shilitu {
		width: 134rpx;
		height: 128rpx;
		border-radius: 14rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 14rpx;
		}
	}

	.biabox {
		height: 168rpx;
	}

	.dhtext {
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FAB536;
	}

	.huisetext {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.yhxiank {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
</style>
